﻿using DapperExtensions;
using DapperExtensions.Mapper;
using DapperExtensions.Sql;
using WMLogistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Dapper;

namespace WMLogistics_Dao
{
    public class WMBidWLPriceDao
    {
        public WMBidWLPriceDao()
        {
            var config = new DapperExtensionsConfiguration(typeof(AutoClassMapper<>), new List<Assembly>(), new MySqlDialect());
            var sqlGenerator = new SqlGeneratorImpl(config);
            DapperExtensions.DapperExtensions.Configure(config);
        }

        /// <summary>
        /// 获取标单物流报价表数据
        /// </summary>
        /// <param name="bid_id"></param>
        /// <returns></returns>
        public IEnumerable<wuliu_wm_bid_wlcompany> GetPriceList(int bid_id)
        {
            IList<ISort> sort = new List<ISort>
                                    {
                                        Predicates.Sort<wuliu_wm_bid_wlcompany>(p => p.tax_price1,true),
                                        Predicates.Sort<wuliu_wm_bid_wlcompany>(p => p.create_time,true)
                                    };
            using (var conn = ConnectionHelper.OpenConnection())
            {
                var bid_id_pre = Predicates.Field<wuliu_wm_bid_wlcompany>(f => f.bid_id, Operator.Eq, bid_id);
                var state_pre = Predicates.Field<wuliu_wm_bid_wlcompany>(f => f.state, Operator.Eq, 50);//状态：1中标，50未中标
                var flag_pre = Predicates.Field<wuliu_wm_bid_wlcompany>(f => f.flag, Operator.Eq, 1);
              var pre=  Predicates.Group(GroupOperator.And, bid_id_pre, state_pre, flag_pre);
              return conn.GetList<wuliu_wm_bid_wlcompany>(pre,sort);
            }
        }

        /// <summary>
        /// 获取重新最终报价的列表
        /// </summary>
        /// <param name="bid_id"></param>
        /// <returns></returns>
        public IEnumerable<wuliu_wm_bid_wlcompany> GetRePriceList(int bid_id)
        {
            IList<ISort> sort = new List<ISort>
                                    {
                                        Predicates.Sort<wuliu_wm_bid_wlcompany>(p => p.tax_price1,true),
                                        Predicates.Sort<wuliu_wm_bid_wlcompany>(p => p.create_time,true)
                                    };
            using (var conn = ConnectionHelper.OpenConnection())
            {
                var bid_id_pre = Predicates.Field<wuliu_wm_bid_wlcompany>(f => f.bid_id, Operator.Eq, bid_id);
                var bid_state = Predicates.Field<wuliu_wm_bid_wlcompany>(f => f.state, Operator.Eq, 50);
                var pre = Predicates.Group(GroupOperator.And, bid_id_pre, bid_state);
                return conn.GetList<wuliu_wm_bid_wlcompany>(pre, sort);
            }
        }

        /// <summary>
        /// 获取最终报价列表
        /// </summary>
        /// <param name="bid_id"></param>
        /// <returns></returns>
        public IEnumerable<wuliu_wm_bid_wlcompany> GetBillPriceList(int bid_id)
        {
            IList<ISort> sort = new List<ISort>
                                    {
                                        Predicates.Sort<wuliu_wm_bid_wlcompany>(p => p.tax_price1,true),
                                         Predicates.Sort<wuliu_wm_bid_wlcompany>(p => p.create_time,true)
                                    };
            using (var conn = ConnectionHelper.OpenConnection())
            {
                var bid_id_pre = Predicates.Field<wuliu_wm_bid_wlcompany>(f => f.bid_id, Operator.Eq, bid_id);


                var pre = Predicates.Group(GroupOperator.And, bid_id_pre);
                return conn.GetList<wuliu_wm_bid_wlcompany>(pre, sort);
            }
        }

       

        /// <summary>
        /// 第一次选定标书
        /// </summary>
        /// <param name="price"></param>
        /// <returns></returns>
        public bool ChoiceBidPrice(int bid_id, int bid_wl_id, wuliu_wm_bid_wlcompany pricemod,wuliu_wm_bid_load load)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                WMBidBaseDao bidbase = new WMBidBaseDao();
                WMBidLoadDao dao = new WMBidLoadDao();
                wuliu_wm_bid_base bid = new wuliu_wm_bid_base();
                bid.bid_id = bid_id;
                bid.wuliu_id = load.wuliu_id;
                bid.wuliu_name = load.wuliu_name;
                bid.wuliu_no = load.wuliu_no;
                bid.price = pricemod.price;
                bid.tax1 = pricemod.tax1;//税率
                bid.tax_price1 = pricemod.tax_price1;
                bid.tax2 = pricemod.tax2;
                bid.tax_price2 = pricemod.tax_price2;
                bid.state = 100;
                bid.bid_time = DateTimeToStamp(DateTime.Now);
                int num = bidbase.BidBaseUpdate(bid);//标单状态更新选标中
                //插入运单数据
                dao.Add(load);
                string sql = "update wuliu_wm_bid_wlcompany set state=1,flag=10,bid_flag=1 where  bid_wl_id=@bid_wl_id";//物流选中
                int nu= conn.Execute(sql, new { bid_wl_id });
                return nu == 1 && num == 1;

            }
        }

        private int DateTimeToStamp(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }

        /// <summary>
        /// 重新选标操作
        /// </summary>
        /// <param name="bid_id"></param>
        /// <param name="bid_wl_id"></param>
        /// <param name="load"></param>
        /// <returns></returns>
        public bool ChoiceReBidPrice(int bid_id, int bid_wl_id, wuliu_wm_bid_wlcompany pricemod, wuliu_wm_bid_load load)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                WMBidBaseDao bidbase = new WMBidBaseDao(); WMBidLoadDao dao = new WMBidLoadDao();
                wuliu_wm_bid_base bid = new wuliu_wm_bid_base();
                bid.bid_id = bid_id;
                bid.wuliu_id = load.wuliu_id;
                bid.wuliu_name = load.wuliu_name;
                bid.wuliu_no = load.wuliu_no;
                bid.tax1 = pricemod.tax1;
                bid.tax_price1 = pricemod.tax_price1;
                bid.price = pricemod.price;
                bid.state = 100;
                bid.bid_time = DateTimeToStamp(DateTime.Now);
                int num = bidbase.BidBaseUpdate(bid);//标单状态更新选标中
                //插入运单数据
                dao.Add(load);
                string sql = "update wuliu_wm_bid_wlcompany set state=1,flag=10,bid_flag=0 where  bid_wl_id=@bid_wl_id";//物流选中
                int nu = conn.Execute(sql, new { bid_wl_id });
                return nu == 1 && num == 1;

            }
        }
      
        /// <summary>
        /// 把原先选中的物流企业更新成（ flag=10曾经被选中，bid_flag=20 重新取货拒单）
        /// </summary>
        /// <param name="bid_id"></param>
        /// <returns></returns>
        public bool NotPickUp(int bid_id)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                WMBidBaseDao bidbase = new WMBidBaseDao();
                string sql = "update wuliu_wm_bid_wlcompany set bid_flag=20,flag=10,state=1 where bid_flag=1 and bid_id=@bid_id";
                return conn.Execute(sql, new { bid_id }) == 1;

            }
        }

        /// <summary>
        /// 运单关闭
        /// </summary>
        /// <returns></returns>
        public bool UpLoadSate(string loadno)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                WMBidBaseDao bidbase = new WMBidBaseDao();
                string sql = "update wuliu_wm_bid_load set load_state=100 where load_no=@loadno";
                //string sql = "update wuliu_wm_bid_load set bid_flag=10 where load_no=@loadno";
                return conn.Execute(sql, new { loadno }) == 1;

            }
        }

        //load_state

        /// <summary>
        /// 获取最终报价表
        /// </summary>
        /// <param name="wuliu_id"></param>
        /// <returns></returns>
        public wuliu_wm_bid_wlcompany GetByWuLiuId(string billNo, int wuliu_id)
        {
            DynamicParameters dynamicParameters = new DynamicParameters();
            var sql = "select * from wuliu_wm_bid_wlcompany  where bid_no=@bid_no and wuliu_id=@wuliu_id and bid_flag=1 and state=1";
            dynamicParameters.Add("wuliu_id", wuliu_id);
            dynamicParameters.Add("bid_no", billNo);
            using (var conn = ConnectionHelper.OpenConnection())
            {
                return conn.Query<wuliu_wm_bid_wlcompany>(sql, dynamicParameters).ToList().FirstOrDefault();
            }
            //using (var conn = ConnectionHelper.OpenConnection())
            //{
            //    return conn.Get<wuliu_wm_bid_wlcompany>(wuliu_id);
            //}
        }

        public wuliu_wm_bid_wlcompany Get(int bid_wl_id)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                return conn.Get<wuliu_wm_bid_wlcompany>(bid_wl_id);
            }
        }
    }
}
